package com.bac.rentmap.web.controller.geolocation;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import com.bac.rentmap.ad.flat.GeoLocation;
import com.bac.rentmap.manager.BuildingManager;
import com.bac.rentmap.model.Building;
import com.google.appengine.api.datastore.GeoPt;

/**
 * Provides label for location to display on UI.
 * 
 * @author Andrew Osipenko
 */
public class GeoLocationLabelController extends AbstractCommandController {
	private static final Logger log = Logger
			.getLogger(GeoLocationLabelController.class.getName());
	private BuildingManager buildingManager;

	@Override
	protected ModelAndView handle(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		GeoLocation geoLocation = (GeoLocation) command;
		log.info("Evaluating label for " + geoLocation);
		Building building = buildingManager.findNearestBuilding("minsk",
				new GeoPt(geoLocation.getLat().floatValue(), geoLocation
						.getLon().floatValue()));
		Map model = new HashMap();
		model.put("building", building);
		return new ModelAndView("easysearch/geoLocation/label", model);
	}

	@Required
	public void setBuildingManager(BuildingManager buildingManager) {
		this.buildingManager = buildingManager;
	}
}
